//
//  DeleteTagCommon.swift
//  JKConsole
//
//  Created by 姜奎 on 2020/11/20.
//  Copyright © 2020 JK. All rights reserved.
//

import Foundation

class DeleteTagCommon: PullCommon {
    override func run(options: ConsoleOptions) {
        let pro = Project()
        let project = Project(directoryPath: pro.rootPath)
        if pro.rootPath != pro.directoryPath {
            self.deleteTag(pro: project, options: options)
        }
        do {
            let data = try Data(contentsOf: URL(fileURLWithPath: project.recordListPath))
            let recordList = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! Array<String>
            print(Colors.green("【\(project.name)】Modulefile.recordList 读取成功"))
            for record in recordList {
                let pro1 = Project(directoryPath: "\(project.checkoutsPath)/\(record)")
                self.deleteTag(pro: pro1, options: options)
            }
        } catch {
            print(Colors.red("【\(project.name)】Modulefile.recordList 读取失败"))
        }
    }
    
    func deleteTag(pro: Project,options: ConsoleOptions) {
        guard let tag = options.tag else {
            print(Colors.red("扩展参数-v：【tag】"))
            exit(EXIT_FAILURE)
        }
        do {
            print(Colors.green("【\(pro.name)】pull 开始"))
            let date = Date.init().timeIntervalSince1970
            try shellOut(to: .gitDelTag(tag: tag),at: pro.directoryPath)
            print(Colors.green("【\(pro.name)】:用时：" + String(format: "%.2f", Date.init().timeIntervalSince1970-date) + "s"))
        } catch {
            print(Colors.red("【\(pro.name)】pull 失败"))
            let error = error as! ShellOutError
            print(error.message) // Prints STDERR
            print(error.output) // Prints STDOUT
        }
    }
}
